﻿using ManYan.Common;
using ManYan.Common.Intercept;
using ManYan.Domain.DomainService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

using Castle.DynamicProxy;

namespace ManYan.Application
{
    public class BaseAppService<T> : IBaseAppService<T> where T : class, new()
    {
        private readonly IBaseDomainService<T> _baseDomainService;
        public BaseAppService(IBaseDomainService<T> baseDomainService)
        {
            _baseDomainService = baseDomainService;
        }
        public Res AddEntity(T entity)
        {
            bool res = _baseDomainService.AddEntity(entity);
            if (res == true)
                return new Res() { Code = 200, Msg = "添加成功", Data = true };
            else
                return new Res() { Code = 500, Msg = "添加失败", Data = true };
        }

        public int DelEntity(Expression<Func<T, bool>> where)
        {
            

            return _baseDomainService.DelEntity(where);
        }

        public int GetEntitiesCount(Expression<Func<T, bool>> where)
        {
            return _baseDomainService.GetEntitiesCount(where);
        }

        public List<T> LoadEntityAll()
        {
            return _baseDomainService.LoadEntityAll();
        }

        public Res<List<T>> LoadEntityEnumerable(Expression<Func<T, bool>> where, Expression<Func<T, DateTime?>> orderby, string asc, int pageIndex, int pageSize)
        {
            var count = _baseDomainService.GetEntitiesCount(where);
            return new Res<List<T>>() { Msg = "获取数据成功", Code = 200, Data = _baseDomainService.LoadEntityEnumerable(where, orderby, asc, pageIndex, pageSize).ToList(), Count = count };

        }



        public Res WhereLoadEntity(Expression<Func<T, bool>> where)
        {
            var data = _baseDomainService.WhereLoadEntity(where);
            return new Res() { Code = 200, Msg = "数据获取成功", Data = data };
        }
        public T WhereLoadEntityT(Expression<Func<T, bool>> where)
        {
            var data = _baseDomainService.WhereLoadEntity(where);
            return data;
        }



        public Res UpdateEntity(T entity)
        {
            return new Res() { Data = _baseDomainService.UpdateEntity(entity), Code = 200, Msg = "添加成功" };
        }

        public Res SuccessMsg(T entity)
        {
            return new Res() { Code = 200, Msg = "数据获取成功", Data = entity };
        }

        public Res ErrorMsg(T? entity)
        {
            return new Res() { Code = 501, Msg = "数据获取失败", Data = entity };
        }

        public Res SuccessMsg(List<T> entity)
        {
            return new Res() { Code = 200, Msg = "数据获取成功", Data = entity };
        }

        public Res ErrorMsg(List<T>? entity)
        {
            return new Res() { Code = 501, Msg = "数据获取失败", Data = entity };
        }
    }
}
